package com.ithuameng.admin.app.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ithuameng.admin.app.system.dao.RoleMenuMapper;
import com.ithuameng.admin.app.system.model.RoleMenu;
import com.ithuameng.admin.app.system.service.IRoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * User service implementation
 *
 * @author ithuameng
 */
@Transactional
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements IRoleMenuService {

    @Override
    public boolean checkMenuExistRole(Long menuId) {
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(RoleMenu.MENU_ID, menuId);
        int count = super.count(queryWrapper);
        return count > 0;
    }

    @Override
    public int countRoleMenus(Long roleId) {
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(RoleMenu.ROLE_ID, roleId);
        return super.count(queryWrapper);
    }

    @Override
    public void deleteByRoleId(Long[] roleIds) {
        if (roleIds == null || roleIds.length == 0) {
            return;
        }
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(RoleMenu.ROLE_ID, Arrays.asList(roleIds));
        super.remove(queryWrapper);
    }
}
